//
//  DetailOfPageViewController.m
//  PetAPP
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "DetailOfPageViewController.h"
#import "NetworkingHandle.h"
#import "ModelOfDetailOfPage.h"
#import "ModelOfPost.h"
#import "DetailFirstCell.h"
#import "DetailOtherCell.h"
#import "CellOfDetail.h"

@interface DetailOfPageViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) ModelOfDetailOfPage *detailOfModel;

@property (nonatomic, retain) UICollectionView *collection;

@property (nonatomic, retain) NSMutableArray *postArr;
@end

@implementation DetailOfPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    
    [self changeNavigationBar];
    [self handleData];
    [self createTextField];
    
}

- (void)createTextField {
    UIView *viewOfText = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 60)];
    [self.view addSubview:viewOfText];
    [viewOfText release];
    
    
    

}

- (void)changeNavigationBar {
    self.navigationItem.title = @"帖子详情";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor brownColor],
        NSFontAttributeName : [UIFont systemFontOfSize:22]};
    
    
}

- (void)handleData {
    
    NSString *url = @"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=threadDetailNew";
    
    NSString *body = [NSString stringWithFormat:@"service=threadDetailNew&sign=c0bd37f1a5336b50cb439c9605900f6d&tid=%@&version=d1bd83a33f1a841ab7fda32449746cc4", self.tid];
    
    NSString *cookie = [NSString stringWithFormat:@"leepetphone%%5Bpassword%%5D=%@; leepetphone[source]=ios; leepetphone[uid]=%@; leepetphone[username]=%@", [[NSUserDefaults standardUserDefaults] stringForKey:@"password"], [[NSUserDefaults standardUserDefaults] stringForKey:@"uid"], [[[NSUserDefaults standardUserDefaults] stringForKey:@"username"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    if (![[[NSUserDefaults standardUserDefaults] stringForKey:@"loginState"] boolValue]) {
        cookie = @"";
    }
    NSDictionary *dic = @{@"Content-Type" : @"application/x-www-form-urlencoded", @"Cookie" : cookie};
    
    [NetworkingHandle handleWorkData:url AndBody:body value:dic handleBlock:^(id result) {
        
        NSDictionary *tempDic = [result objectForKey:@"threadinfo"];
        
        _detailOfModel = [[ModelOfDetailOfPage alloc] init];
        [_detailOfModel setValuesForKeysWithDictionary:tempDic];
        /** 点赞的用户宠物信息 */
        _detailOfModel.recommendArr = [result objectForKey:@"recommendarray"];
        
        /** 处理评论数据 */
        self.postArr = [NSMutableArray array];
        NSArray *tempArr = [result objectForKey:@"postarray"];
        
        for (NSDictionary *dic in tempArr) {
            ModelOfPost *model = [[ModelOfPost alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.postArr addObject:model];
            [model release];
        }
        [self createSubViews];
        
        
    }];
    
}

- (void)createSubViews {
    
    CGFloat width = self.view.frame.size.width;
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    
    flowLayout.itemSize = CGSizeMake(width, 100);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(width, 200);
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 0, 30, 0);
    
    

    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.8489 green:0.8489 blue:0.8489 alpha:1.0], [UIColor whiteColor]);
    self.collection.showsVerticalScrollIndicator = NO;
    
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [_collection release];
    
    
    
    [self.collection registerClass:[CellOfDetail class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfHeader"];
    
    
    [self.collection registerClass:[DetailFirstCell class] forCellWithReuseIdentifier:@"poolOfFirst"];
    [self.collection registerClass:[DetailOtherCell class] forCellWithReuseIdentifier:@"poolOfOther"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.postArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.item == 0) {
        DetailFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfFirst" forIndexPath:indexPath];
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
        
        [cell handleDataWithModel:self.postArr[self.postArr.count - 1 - indexPath.item] andNumber:self.postArr.count];
        
        
        return cell;
    }else{
         DetailOtherCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfOther" forIndexPath:indexPath];
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
        
        [cell handleDataWithModel:self.postArr[self.postArr.count - 1 - indexPath.item]];
        
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    ModelOfPost *model = self.postArr[self.postArr.count - 1 - indexPath.item];
    CGRect contentRect = [[model.message[0] objectForKey:@"content"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 65, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    if (indexPath.item == 0) {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20 + 91 + 10);
    }else {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20 + 51 + 10);
    }
    
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    CellOfDetail *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolOfHeader" forIndexPath:indexPath];
    [headerView handleDataWithModel:self.detailOfModel];
    
    return headerView;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, [CellOfDetail handleForHeightWithModel:self.detailOfModel]);
}





- (void)dealloc {
    [_tid release];
    [_postArr release];
    [_collection release];
    [_detailOfModel release];
    [super dealloc];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
